//
//  YGQuotationSubsriber.m
//  luGongPro
//
//  Created by 王庆学 on 2017/8/23.
//  Copyright © 2017年 王庆学. All rights reserved.
//

#import "YGQuotationSubsriber.h"
#import "EFSChartSocketManager.h"

@interface YGQuotationSubsriber ()

@property (nonatomic, strong) NSMutableSet *subscribeIDs;

@property (nonatomic, strong) NSMutableSet *subscribingIDs;

@end

@implementation YGQuotationSubsriber

+ (instancetype)defaultSubscriber{
    static YGQuotationSubsriber *subsricer = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        subsricer = [[self alloc]init];
    });
    return subsricer;
}

- (instancetype)init{
    if (self = [super init]){
        _subscribeIDs = [[NSMutableSet alloc]init];
        _subscribingIDs = [[NSMutableSet alloc]init];
    }
    return self;
}

- (void)addSubsribeWithQuotationID:(NSString *)quotationID{
    if (quotationID){
        if (![self.subscribeIDs containsObject:quotationID]){
            [self.subscribeIDs addObject:quotationID];
        }
    }
}

- (void)addSubsribeWithQuotationIds:(NSArray<NSString *> *)quotationIDs{
    [self.subscribeIDs addObjectsFromArray:quotationIDs];
}

- (void)removeSubscribeWithQuotationID:(NSString *)quotationID{
    if (quotationID){
        [self.subscribeIDs removeObject:quotationID];
    }
}

- (void)removeSubscribeWithQuotationIDs:(NSArray<NSString *> *)quotationIDs{
    [quotationIDs enumerateObjectsUsingBlock:^(NSString * _Nonnull quotationID, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.subscribeIDs removeObject:quotationID];
    }];
}

- (void)removeAllSubscribe{
    [self.subscribeIDs removeAllObjects];
}

- (void)suspend{
    [self.subscribeIDs enumerateObjectsUsingBlock:^(NSString * _Nonnull quotationID, BOOL * _Nonnull stop) {
        [[EFSChartSocketManager sharedChartSocketController] unsubscribeWithID:quotationID];
    }];
    [self.subscribeIDs removeAllObjects];
    [self log];
}

- (void)resume{
    [self.subscribeIDs enumerateObjectsUsingBlock:^(NSString * _Nonnull quotationID, BOOL * _Nonnull stop) {
        if (![self.subscribeIDs containsObject:quotationID]){
            [[EFSChartSocketManager sharedChartSocketController] unsubscribeWithID:quotationID];
            [self.subscribeIDs removeObject:quotationID];
        }
    }];
    
    [self.subscribeIDs enumerateObjectsUsingBlock:^(NSString * _Nonnull quotationID, BOOL * _Nonnull stop) {
        if (![self.subscribeIDs containsObject:quotationID]){
            [[EFSChartSocketManager sharedChartSocketController] subscribeWithID:quotationID];
            [self.subscribeIDs addObject:quotationID];
        }
    }];
    [self log];
}

- (void)cancel{
    [self.subscribeIDs enumerateObjectsUsingBlock:^(NSString * _Nonnull  quotationID, BOOL * _Nonnull stop) {
        [[EFSChartSocketManager sharedChartSocketController] unsubscribeWithID:quotationID];
    }];
    [self.subscribeIDs removeAllObjects];
    [self removeAllSubscribe];
    [self log];
}

- (void)log {
#if DEBUG
    LGlog(@"订阅中：subscribingIDs%@-----------count_%ld",self.subscribeIDs,self.subscribingIDs.count);
#endif
}

@end
